package edu.polsl.bio.str.node.logic;

import edu.polsl.bio.str.common.exception.ManagerException;
import edu.polsl.bio.str.node.delegate.NodeManagementServiceDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jul 18, 2011 2:38:28 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class HeartbeatDispatcherImpl implements HeartbeatDispatcher {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(HeartbeatDispatcherImpl.class.getName());

  private NodeManagementServiceDelegate nodeManagementServiceDelegate;

  private NodeContext nodeContext;

  private ConnectionLossHandler connectionLossHandler;

  @Override
  public void dispatchHeartbeatSignal() {
    if (nodeContext.isAttached()) {
      log.debug("Dispatching heartbeat...");
      try {
        nodeManagementServiceDelegate.heartBeat(nodeContext.getNodeId());
      } catch (ManagerException e) {
        log.error("Exception occurred during dispatching heartbeat", e);
        connectionLossHandler.handleConnectionLoss();
      }
    }
  }

  public void setNodeManagementServiceDelegate(NodeManagementServiceDelegate nodeManagementServiceDelegate) {
    this.nodeManagementServiceDelegate = nodeManagementServiceDelegate;
  }

  public void setNodeContext(NodeContext nodeContext) {
    this.nodeContext = nodeContext;
  }

  public void setConnectionLossHandler(ConnectionLossHandler connectionLossHandler) {
    this.connectionLossHandler = connectionLossHandler;
  }
}
